package com.wang.mandkey.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.wang.mandkey.R;
import com.wang.mandkey.tool.RemoteOperateImpl;
import com.wang.mandkey.tool.Tool;
import com.wang.mandkey.utils.ProjectEnvironment;

public class MouseView extends SurfaceView implements SurfaceHolder.Callback,
		Runnable {

	private Point point = ProjectEnvironment.pointHostScreen;
	private Point locationPoint = new Point();
	private RemoteOperateImpl remoteOperateImpl = null;
	private Context mContext;
	private Drawable mDrawbleBackground = null;
	private Bitmap mBitmapbackGround = null;
	private SurfaceHolder mSurfaceHolder;
	private Paint paint = null;
	private Thread thread;
	private Bitmap bitmapArrow;

	public MouseView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		paint = new Paint();
		thread = new Thread(this);
		thread.start();
		this.setFocusable(true);
		bitmapArrow = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.arrow);
	}

	public synchronized boolean Draw() {
		if (null == mSurfaceHolder) {
			return false;
		}
		Canvas canvas = mSurfaceHolder.lockCanvas();
		if (null == canvas) {
			return false;
		}
		if (true) {// 确保只有在图片更新完成之后才更新背景图片
			if (null != mBitmapbackGround) {
				Bitmap mBitmap = Bitmap.createScaledBitmap(mBitmapbackGround,
						ProjectEnvironment.INT_AFTER_SCALE_X,
						ProjectEnvironment.INT_AFTER_SCALE_Y, true);
				canvas.drawBitmap(mBitmap, 0, 0, paint);
			} else {
				canvas.drawColor(0xffffffff);
				canvas.drawLine(0, Tool.getDisplayMetrics(mContext).y - 60,
						Tool.getDisplayMetrics(mContext).x,
						Tool.getDisplayMetrics(mContext).y - 60, paint);
			}
		}

		paint.setColor(Color.BLUE);
		if (null != point) {
			canvas.drawText(
					"点击菜单有惊喜,电脑屏幕:("
							+ point.x
							+ "*"
							+ point.y
							+ ")手机屏幕:("
							+ Tool.getDisplayMetrics(mContext).x
							+ "*"
							+ Tool.getDisplayMetrics(mContext).y
							+ ")鼠标位置:("
							+ locationPoint.x
							+ ","
							+ locationPoint.y
							+ ") "
							+ mContext.getResources().getString(
									R.string.mouse_activity_compute_scale_fit)
							+ ":" + ProjectEnvironment.FLOAT_SCALE, 20, 20,
					paint);
			// bitmapArrow

			canvas.drawBitmap(bitmapArrow, locationPoint.x, locationPoint.y,
					paint);

		} else {
			canvas.drawText(
					this.mContext.getResources().getString(
							R.string.mouse_activity_menu_link_first), 20, 20,
					paint);
		}
		mSurfaceHolder.unlockCanvasAndPost(canvas);
		return true;// 方法结束的时候通知绘图线程可以重新绘制了
	}

	long actionDown = 0;
	long actionUp = 0;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();

		float x = event.getX();
		float y = event.getY();

		locationPoint.x = (int) x;
		locationPoint.y = (int) y;
		switch (action) {

		case MotionEvent.ACTION_DOWN:
			actionDown = System.currentTimeMillis();
//			 remoteOperateImpl.mouseDown(x, y);
			if ((actionUp - actionDown) < 300) {
				remoteOperateImpl.click(x, y);
			}
			break;
		case MotionEvent.ACTION_MOVE:
			// Bitmap cache = getDrawingCache();

			float rx = event.getRawX();
			float ry = event.getRawY();

			remoteOperateImpl.moveMouse(x, y);
			break;
		case MotionEvent.ACTION_UP:
			// remoteOperateImpl.mouseUp(x, y);
//			actionUp = System.currentTimeMillis();
//			if ((actionUp - actionDown) < 300) {
//				remoteOperateImpl.click(x, y);
//			}

			break;
		default:
			break;
		}

		return true;
	}

	public Point getPoint() {
		return point;
	}

	public void setPoint(Point point) {
		this.point = point;
	}

	public Point getLocationPoint() {
		return locationPoint;
	}

	public void setLocationPoint(Point locationPoint) {
		this.locationPoint = locationPoint;
	}

	public RemoteOperateImpl getRemoteOperateImpl() {
		return remoteOperateImpl;
	}

	public void setRemoteOperateImpl(RemoteOperateImpl remoteOperateImpl) {
		this.remoteOperateImpl = remoteOperateImpl;
	}

	public Drawable getmDrawbleBackground() {
		return mDrawbleBackground;
	}

	public void setmDrawbleBackground(Drawable mDrawbleBackground) {
		this.mDrawbleBackground = mDrawbleBackground;
	}

	public Bitmap getmBitmapbackGround() {
		return mBitmapbackGround;
	}

	public void setmBitmapbackGround(Bitmap mBitmapbackGround) {
		Draw();
		this.mBitmapbackGround = mBitmapbackGround;
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
	}

	@Override
	public void run() {
		while (true) {
			Draw();
			Tool.delay(100);
		}
	}

}
